package com.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.domain.RegistrationInformation;
import com.demo.domain.TeamProfile;
import com.demo.domain.ro.AddRegInfoRequest;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface RegistrationInformationDao extends BaseMapper<RegistrationInformation> {
    @Update("UPDATE tb_registration_information ri set ri.ri_del_flag = 1 where ri.ri_id = #{riId}")
    int deleteRegistrationInformationById(RegistrationInformation registrationInformation);

    @Select("SELECT * FROM tb_registration_information ri where ri.ri_del_flag = 0")
    List<RegistrationInformation> listAllRegistrationInformation();


    @Select("<script>" +
            "SELECT * FROM tb_registration_information ri " +
            "where ri.ri_del_flag = 0" +
            "<if test='riUid != null and riUid != \"\"'>AND ri.ri_uid = #{riUid}</if>" +
            "<if test='riUnickname != null and riUnickname != \"\"'>AND ri.ri_unickname = #{riUnickname}</if>" +
            "<if test='riRealname != null and riRealname != \"\"'>AND ri.ri_realname = #{riRealname}</if>" +
            "<if test='riContact != null and riContact != \"\"'>AND ri.ri_contact = #{riContact}</if>" +
            "<if test='riReviewStatus != null and riReviewStatus != \"\"'>AND ri.ri_review_status = #{riReviewStatus}</if>" +
            "<if test='riTeamId != null and riTeamId != \"\"'>AND ri.ri_team_id = #{riTeamId}</if>" +
            " ORDER BY ri.ri_id ASC" +
            "</script>")
    List<RegistrationInformation> selectRegistrationInformationList(Long riUid, String riUnickname, String riRealname, String riContact, Integer riReviewStatus, Long riTeamId);



    @Select("SELECT ri.ri_id,ri.ri_team_id,tp.tp_team_name,tp.tp_uid,u.u_realname,ri.ri_review_status,ri.ri_apply_time,ri.ri_check_time,ri.ri_cancel_time,ri_had_viewed  \n" +
            "FROM `tb_registration_information` as ri, tb_user as u,tb_team_profile as tp\n" +
            "WHERE ri.ri_team_id=tp.tp_id and tp.tp_uid=u.u_id and ri.ri_uid=#{uid} and tp.tp_alive=1\n" +
            "ORDER BY   \n" +
            "    CASE   \n" +
            "        WHEN ri_had_viewed = 0 AND (ri_review_status = 1 OR ri_review_status = 2) THEN 0    \n" +
            "        ELSE 1  \n" +
            "    END," +
            "ri_had_viewed ASC,  \n" +
            "    ri_review_status ASC")
    List<Map<String, Object>> selectRegistrationsToDisplayByUid(Long uid);

    @Select("SELECT COUNT(*) AS total_count  \n" +
            "FROM tb_registration_information  \n" +
            "WHERE ri_had_viewed = 0 AND (ri_review_status = 1 OR ri_review_status = 2) " +
            "AND ri_uid=#{uid};")
    int countNotViewRegistration(Long uid);

    @Update("UPDATE tb_registration_information ri SET ri.ri_review_status=1 WHERE ri.ri_id=#{id}")
    boolean passRegistration(Long id);

    @Update("UPDATE tb_registration_information ri SET ri.ri_review_status=2 WHERE ri.ri_id=#{id}")
    boolean failRegistration(Long id);

    @Update("UPDATE tb_registration_information\n" +
            "SET ri_had_viewed = 1\n" +
            "WHERE ri_uid=#{uid}")
    int updateHadViewed(Long uid);

    /**
     * 插入记录，数据库生成主键
     * 报名队伍
     *
     * @param addRegInfoRequest
     * @return
     */
    @Insert("INSERT INTO tb_registration_information (ri_uid,ri_unickname, ri_realname, ri_grade, "
            + "ri_major, ri_contact, ri_intro, "
            + "ri_team_id,ri_apply_time,ri_review_status) "
            + "VALUES "
            + "(#{ri_uid},#{ri_unickname},#{ri_realname},#{ri_grade},#{ri_major},#{ri_contact},"
            + "#{ri_intro},#{ri_team_id},"
            + "#{ri_apply_time},#{ri_review_status})")
    public Integer addRegInf(AddRegInfoRequest addRegInfoRequest);

}
